package com.example.fanpaiyouxi

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    private lateinit var tvName: TextView
    private lateinit var tvGender: TextView
    private lateinit var tvPhone: TextView
    private lateinit var tvHighScore: TextView
    private lateinit var btnEdit: Button
    private lateinit var btnGame: Button

    private var playerInfo = PlayerInfo("张三", "男", "13800138000", 0)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initViews()
        setupClickListeners()
        displayPlayerInfo()
    }

    override fun onResume() {
        super.onResume()
        // 从游戏Activity返回时更新最高分
        val highScore = intent.getIntExtra("high_score", playerInfo.highScore)
        if (highScore > playerInfo.highScore) {
            playerInfo.highScore = highScore
        }
        displayPlayerInfo()
    }

    private fun initViews() {
        tvName = findViewById(R.id.tvName)
        tvGender = findViewById(R.id.tvGender)
        tvPhone = findViewById(R.id.tvPhone)
        tvHighScore = findViewById(R.id.tvHighScore)
        btnEdit = findViewById(R.id.btnEdit)
        btnGame = findViewById(R.id.btnGame)
    }

    private fun setupClickListeners() {
        btnEdit.setOnClickListener {
            val intent = Intent(this, EditInfoActivity::class.java).apply {
                putExtra("player_info", playerInfo)
            }
            startActivityForResult(intent, REQUEST_EDIT_INFO)
        }

        btnGame.setOnClickListener {
            val intent = Intent(this, CardGameActivity::class.java).apply {
                putExtra("high_score", playerInfo.highScore)
            }
            startActivity(intent)
        }
    }

    private fun displayPlayerInfo() {
        tvName.text = getString(R.string.player_name) + ": " + playerInfo.name
        tvGender.text = getString(R.string.player_gender) + ": " + playerInfo.gender
        tvPhone.text = getString(R.string.player_phone) + ": " + playerInfo.phone
        tvHighScore.text = getString(R.string.player_high_score) + ": " + playerInfo.highScore
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_EDIT_INFO && resultCode == RESULT_OK) {
            data?.getParcelableExtra<PlayerInfo>("updated_info")?.let {
                playerInfo = it
                displayPlayerInfo()
            }
        }
    }

    companion object {
        const val REQUEST_EDIT_INFO = 1001
    }
}

data class PlayerInfo(
    var name: String,
    var gender: String,
    var phone: String,
    var highScore: Int
) : android.os.Parcelable {
    constructor(parcel: android.os.Parcel) : this(
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readInt()
    )

    override fun writeToParcel(parcel: android.os.Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeString(gender)
        parcel.writeString(phone)
        parcel.writeInt(highScore)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : android.os.Parcelable.Creator<PlayerInfo> {
        override fun createFromParcel(parcel: android.os.Parcel): PlayerInfo {
            return PlayerInfo(parcel)
        }

        override fun newArray(size: Int): Array<PlayerInfo?> {
            return arrayOfNulls(size)
        }
    }
}